package com.guigu.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.guigu.base.entity.TransportTripsTruckDriver;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.base.mapper.TransportTripsTruckDriverMapper;
import com.guigu.base.service.TransportTripsTruckDriverService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 车次与车辆和司机关联表 服务实现类
 * </p>
 *
 * @author TaoLong
 * @since 2021-11-08
 */
@Service
public class TransportTripsTruckDriverServiceImpl extends ServiceImpl<TransportTripsTruckDriverMapper, TransportTripsTruckDriver> implements TransportTripsTruckDriverService {

    /**
     * 批量保存车次与车辆关联信息
     *
     * @param truckTransportTripsId
     * @param truckTransportTrips   车次与车辆关联信息
     */
    @Override
    public void batchSave(String truckTransportTripsId, List<TransportTripsTruckDriver> truckTransportTrips) {
        LambdaQueryWrapper<TransportTripsTruckDriver> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TransportTripsTruckDriver::getTransportTripsId, truckTransportTripsId);
        //查出操作前关系列表
        List<TransportTripsTruckDriver> transportTripsTruckDriverList = baseMapper.selectList(lambdaQueryWrapper);
        Map<String, TransportTripsTruckDriver> sourceTruckKeyMap = new HashMap<>();
        for (TransportTripsTruckDriver pdTransportTripsTruckDriver:transportTripsTruckDriverList){
            sourceTruckKeyMap.put(pdTransportTripsTruckDriver.getTransportTripsId() + "_" + pdTransportTripsTruckDriver.getTruckId(),pdTransportTripsTruckDriver);
        }
        //清除关系
        baseMapper.delete(lambdaQueryWrapper);
        List<TransportTripsTruckDriver> saveList = new ArrayList<>();
        //遍历传入数据
        truckTransportTrips.forEach(pdTransportTripsTruckDriver -> {
            TransportTripsTruckDriver saveData = new TransportTripsTruckDriver();
            BeanUtils.copyProperties(pdTransportTripsTruckDriver, saveData);
            if (sourceTruckKeyMap.containsKey(pdTransportTripsTruckDriver.getTransportTripsId() + "_" + pdTransportTripsTruckDriver.getTruckId())) {
                TransportTripsTruckDriver source = sourceTruckKeyMap.get(pdTransportTripsTruckDriver.getTransportTripsId() + "_" + pdTransportTripsTruckDriver.getTruckId());
                if (saveData.getUserId() == null) {
                    saveData.setUserId(source.getUserId());
                }
            }
            saveList.add(saveData);
        });
        saveBatch(saveList);
    }

    /**
     * 获取车次与车辆关联列表
     *
     * @param transportTripsId 车次id
     * @param truckId          车辆Id
     * @param userId           司机id
     * @return 车次与车辆关联列表
     */
    @Override
    public List<TransportTripsTruckDriver> findAll(String transportTripsId, String truckId, String userId) {
        LambdaQueryWrapper<TransportTripsTruckDriver> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(transportTripsId)) {
            lambdaQueryWrapper.eq(TransportTripsTruckDriver::getTransportTripsId, transportTripsId);
        }
        if (StringUtils.isNotEmpty(truckId)) {
            lambdaQueryWrapper.eq(TransportTripsTruckDriver::getTruckId, truckId);
        }
        if (StringUtils.isNotEmpty(userId)) {
            lambdaQueryWrapper.eq(TransportTripsTruckDriver::getUserId, userId);
        }
        return baseMapper.selectList(lambdaQueryWrapper);
    }
}
